package com.cxl.controller;

import com.cxl.entity.Good;
import com.cxl.service.IGoodService;
import com.cxl.springboot.starter.fileupload.FileResult;
import com.cxl.springboot.starter.fileupload.FileUpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: 20971
 * @time: 2022/3/2 14:32
 * @description:
 */

@RestController
@RequestMapping(value = "/good")
public class GoodController {

    @Autowired
    private IGoodService goodService;

    @PostMapping(value = "/add")
    public void ad(@RequestBody Good good) {
        goodService.save(good);
    }

    @GetMapping(value = "/list")
    public Page<Good> goodPage(Good good, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {
//      good.setName("饺子");
        return goodService.queryList(good, pageNum, pageSize);
    }


    @Autowired
    private FileUpLoadService fileUpLoadService;

    /**
     * @description:上传图片
     * @date 2022/2/26
     **/
    @PostMapping(value = "/tofile")
    public FileResult tofile(@RequestParam(value = "file") MultipartFile file) throws IOException {
        return fileUpLoadService.upload(file);
    }


    //删除
    @PostMapping(value = "/del")
    public void del(@RequestBody Good good) {
        goodService.del(good.getId());
    }

}
